<script>
  var lenLongestFibSubseq = function (arr) {
    const n = arr.length;
    // key:num,value:index
    const obj = {};
    let ans = 0;
    for (let key in arr) {
      obj[arr[key]] = Number(key);
    }
    // 答案最少3个,所以2是起点
    const dp = new Array(n).fill(0).map(el => new Array(n).fill(2));
    for (let i = 0; i < n - 1; i++) {
      for (let j = i + 1; j < n; j++) {
        const count = arr[i] + arr[j];
        const index = obj[count];
        if (index) {
          // j,index作为起点的序列是和以i,j作为起点的序列完全重合的，且长度多了一个数。即dp[j][index] = dp[i][j] + 1
          dp[j][index] = dp[i][j] + 1;
          ans = Math.max(dp[j][index], ans);
        }
      }
    }
    return ans;
  };
  console.log(lenLongestFibSubseq([1, 2, 3, 4, 5, 6, 7, 8]));
</script>